<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/3/23
 * Time: 11:24
 */

namespace app\api\model;
use think\Model;
use think\Log;
use think\Db;
use app\common\lib\exception\ApiException;


class Address extends Model
{
    /**
     * @获取省
     * @author 巫玉梅 <1120504614@qq.com>
     * @return ApiException|array|string|\think\response\Json
     */
    public function province(){
        try{
            $info = Db::name('province') ->select();

            return show(config('code.success'),'获取数据成功',$info,200);

        }catch(\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(), 400);
        }
    }
    /**
     * @获取城市
     * @author 巫玉梅 <1120504614@qq.com>
     * @return ApiException|array|string|\think\response\Json
     */
    public function city()
    {
        try {

            $info = Db::name('city')->select();

            return show(config('code.success'), '获取数据成功', $info, 200);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(), 400);
        }
    }
    /**
     * @获取地区
     * @author 巫玉梅 <1120504614@qq.com>
     * @return ApiException|array|string|\think\response\Json
     */
    public function area(){
        try {
            $info = Db::name('area')->select();

            return show(config('code.success'), '获取数据成功', $info, 200);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(), 400);
        }
    }
    /**
     * 获取用户地址详情
     * @param id int 用户id 必传
     * @author 吴铸峰 <1120504614@qq.com>
     * @return \think\response\Json
     */
    public function addressDetail(){

        if(empty($_POST)) {
            throw new ApiException('数据不合法',404);
        }

        if(!isset($_POST['id'])){
            throw new ApiException('id is not found',404);
        }

        try{

            $address=Db::name('address')->where('member_id='.$_POST['id'])->find();
            $code = $address['area_code'];

            $array = Db::name('province')->select();
            $res = [];
            foreach($array as $key => $val){
                $city = Db::name('city')->where('provincecode='.$array[$key]['code'])->select();
                $array[$key]['city'] = $city;
                foreach($array[$key]['city'] as $k => $v){
                    $area = Db::name('area')->where('citycode='.$array[$key]['city'][$k]['code'])->select();
                    $array[$key]['city'][$k]['area'] = $area;
                    foreach($array[$key]['city'][$k]['area'] as $a =>$s) {
                        if ($array[$key]['city'][$k]['area'][$a]['code'] == $code) {
                            $res = [$array[$key]['name'],$array[$key]['city'][$k]['name'],$array[$key]['city'][$k]['area'][$a]['name']];
                        }
                    }
                }
            }

            return show(config('code.success'), '获取数据成功！',$res);

        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }

    /**
     * 地址编码以及地址
     * @param area_code string 区域编码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function addressCode(){
        if(empty($_POST)) {
            throw new ApiException('数据不合法',404);
        }
        // 区域编码
        if(!isset($_POST['area_code'])){
            throw new ApiException('area_code is not found',404);
        }
        try{

            $res = [];

            $array = Db::name('province')->select();
            foreach($array as $key => $val){
                $city = Db::name('city')->where('provincecode='.$array[$key]['code'])->select();

                $array[$key]['city'] = $city;
                foreach($array[$key]['city'] as $k => $v){
                    $area = Db::name('area')->where('citycode='.$array[$key]['city'][$k]['code'])->select();

                    $array[$key]['city'][$k]['area'] = $area;
                    foreach($array[$key]['city'][$k]['area'] as $a =>$s) {
                        if ($array[$key]['city'][$k]['area'][$a]['code'] == $_POST['area_code']) {
                            $res = [
                                'actives' => [$key,$k,$a],
                                'actives_name' => [$array[$key]['name'],$array[$key]['city'][$k]['name'],$array[$key]['city'][$k]['area'][$a]['name']]
                            ];
                        }
                    }
                }
            }

            return show(config('code.success'), 'ok！',$res);

        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }

    }

    /**
     * 修改会员地址
     * @param area_code string 区域编码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function editMemberAddress(){

        if(empty($_POST)) {
            throw new ApiException('数据不合法',404);
        }

        if(!isset($_POST['member_id'])){
            throw new ApiException('member_id is not found',404);
        }

        try{
            $id = $_POST['member_id'];

            $address = Db::name('address');
            $address->where('member_id='.$id)->update($_POST);

            return show(config('code.success'), '操作成功！');

        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }

}